package xin.clips.common.utils;

/**
 * @author
 * @version 进制转换工具
 */
public class HexConvert {

	/**
	 * 字符数组转换成16进制字符串
	 *
	 * @param bytes
	 * @return
	 */
	public static String BinaryToHexString(byte[] bytes) {
		String hexStr = "0123456789ABCDEF";
		String result = "";
		String hex = "";
		for (byte b : bytes) {
			hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
			hex += String.valueOf(hexStr.charAt(b & 0x0F));
			result += hex + " ";
		}
		return result;
	}

	/**
	 * 将一个10进制数据转换成 指定长度的字符串
	 *
	 * @param data
	 * @return
	 */
	public static String getHexData(int data, int len) {
		String str = Integer.toHexString(data) + "";
		if (str.length() >= len) {
			return str;
		} else {
			StringBuffer sb = new StringBuffer("");
			for (int i = 0; i < (len - str.length()); i++) {
				sb.append("0");
			}
			sb.append(str);
			return sb.toString().toUpperCase();
		}
	}

	/**
	 * ascii 转换成16进制数
	 *
	 * @param str
	 * @return
	 */
	public static String convertStringToHex(String str) {

		char[] chars = str.toCharArray();

		StringBuffer hex = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			hex.append(Integer.toHexString((int) chars[i]));
		}

		return hex.toString();
	}

	/**
	 * 字符串转换成16进制byte数组
	 *
	 * @param hexString
	 * @return
	 */
	public static byte[] hexStrToBinaryStr(String hexString) {
		if (hexString == null || "".equals(hexString)) {
			return null;
		}
		hexString = hexString.replaceAll(" ", "");
		int len = hexString.length();
		int index = 0;
		byte[] bytes = new byte[len / 2];
		while (index < len) {
			String sub = hexString.substring(index, index + 2);
			bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
			index += 2;
		}
		return bytes;
	}

	/**
	 * 字符串转换成16进制
	 *
	 * @param s
	 * @return
	 */
	public static String strTo16(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	/**
	 * 两个16进制数据 计算异或
	 *
	 * @param str1 anotherBinary
	 * @param str2 thisBinary
	 * @return
	 */
	public static String xor(String str1, String str2) {
		StringBuffer sb = new StringBuffer();
		int len1 = str1.length(), len2 = str2.length();
		int i = 0, index = 0;
		if (len2 > len1) {
			index = len2 - len1;
			while (i++ < len2 - len1) {
				sb.append(str2.charAt(i - 1));
				str1 = "0" + str1;
			}
		} else if (len1 > len2) {
			index = len1 - len2;
			while (i++ < len1 - len2) {
				sb.append(str1.charAt(i - 1));
				str2 = "0" + str2;
			}
		}
		int len = str1.length();
		while (index < len) {
			int j = (Integer.parseInt(str1.charAt(index) + "", 16) ^ Integer.parseInt(str2.charAt(index) + "", 16))
					& 0xf;
			sb.append(Integer.toHexString(j));
			index++;
		}
		return sb.toString().toUpperCase();

	}

	/**
	 * 16进制数据求反
	 *
	 * @param str
	 * @return
	 */
	public static String getConversely(String str) {
		StringBuffer rerult = new StringBuffer("");
		for (int i = 0; i < str.length(); i++) {
			char s = str.charAt(i);
			if ('0' == s) {
				rerult.append("F");
			} else if ('1' == s) {
				rerult.append("E");
			} else if ('2' == s) {
				rerult.append("D");
			} else if ('3' == s) {
				rerult.append("C");
			} else if ('4' == s) {
				rerult.append("B");
			} else if ('5' == s) {
				rerult.append("A");
			} else if ('6' == s) {
				rerult.append("9");
			} else if ('7' == s) {
				rerult.append("8");
			} else if ('8' == s) {
				rerult.append("7");
			} else if ('9' == s) {
				rerult.append("6");
			} else if ('A' == s || 'a' == s) {
				rerult.append("5");
			} else if ('B' == s || 'b' == s) {
				rerult.append("4");
			} else if ('C' == s || 'c' == s) {
				rerult.append("3");
			} else if ('D' == s || 'd' == s) {
				rerult.append("2");
			} else if ('E' == s || 'e' == s) {
				rerult.append("1");
			} else if ('F' == s || 'f' == s) {
				rerult.append("0");
			}
		}
		return rerult.toString();
	}

	/**
	 * 16进制转换成字符
	 * @param s
	 * @return
	 */
	public static String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "utf-8");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	//16进制转ascii
	public static String convertHexToString(String hex){

		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();

		//49204c6f7665204a617661 split into two characters 49, 20, 4c...
		for( int i=0; i<hex.length()-1; i+=2 ){

			//grab the hex in pairs
			String output = hex.substring(i, (i + 2));
			//convert hex to decimal
			int decimal = Integer.parseInt(output, 16);
			//convert the decimal to character
			sb.append((char)decimal);

			temp.append(decimal);
		}

		return sb.toString();
	}

	//10进制转16进制
	public static String algorismToHEXString(int algorism) {
		String result = "";
		result = Integer.toHexString(algorism);
		if (result.length() % 2 == 1) {
			result = "0" + result;
		}
		result = result.toUpperCase();
		return result;
	}

}
